package utils

// ExchangeArr 交换int数组中的两个元素
func ExchangeIntArr(arr []int, start, end int) []int {
	if start < 0 || end < 0 {
		panic("argument1 and argument2 must be Non-zero")
	}

	if start == end {
		return arr
	}

	arr[start] = arr[end] + arr[start]
	arr[end] = arr[start] - arr[end]
	arr[start] = arr[start] - arr[end]

	return arr
}

// ExchangeStrArr 交换字符数组
func ExchangeStrArr(arr []string, start int, end int) []string {
	if start < 0 || end < 0 {
		panic("argument1 and argument2 must be Non-zero")
	}

	var temp = arr[start]
	arr[start] = arr[end]
	arr[end] = temp

	return arr
}

// MergeArr 合并两个有序的数组
func MergeOrderArr(arr1, arr2 []int) []int {
	var res []int
	p1, p2, len1, len2 := 0, 0, len(arr1), len(arr2)

	for p1 < len1 && p2 < len2 {
		if arr1[p1] < arr2[p2] {
			res = append(res, arr1[p1])
			p1++
		} else {
			res = append(res, arr2[p2])
			p2++
		}
	}

	if p1 >= len(arr1) {
		res = append(res, arr2[p2:len2]...)
	} else {
		res = append(res, arr1[p1:len1]...)
	}

	return res
}
